package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserChannelMapper;
import com.ruoyi.system.domain.SysUserChannel;
import com.ruoyi.system.service.ISysUserChannelService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户入金通道关系Service业务层处理
 *
 * @author Ericcc
 * @date 2021-02-18
 */
@Service
public class SysUserChannelServiceImpl implements ISysUserChannelService
{
    @Autowired
    private SysUserChannelMapper sysUserChannelMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询用户入金通道关系
     *
     * @param userId 用户入金通道关系ID
     * @return 用户入金通道关系
     */
    @Override
    public SysUserChannel selectSysUserChannelById(Long userId)
    {
        return sysUserChannelMapper.selectSysUserChannelById(userId);
    }

    /**
     * 查询用户入金通道关系列表
     *
     * @param sysUserChannel 用户入金通道关系
     * @return 用户入金通道关系
     */
    @Override
    public List<SysUserChannel> selectSysUserChannelList(SysUserChannel sysUserChannel)
    {
        return sysUserChannelMapper.selectSysUserChannelList(sysUserChannel);
    }

    /**
     * 新增用户入金通道关系
     *
     * @param sysUserChannel 用户入金通道关系
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysUserChannel(SysUserChannel sysUserChannel)
    {
        return sysUserChannelMapper.insertSysUserChannel(sysUserChannel);
    }

    /**
     * 修改用户入金通道关系
     *
     * @param sysUserChannel 用户入金通道关系
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysUserChannel(SysUserChannel sysUserChannel)
    {
        SysUser sysUser = sysUserMapper.selectUserById(sysUserChannel.getUserId());
        if (sysUser.getDeptId() == UserType.AGENT.getCode() || sysUser.getDeptId() == UserType.COMMON.getCode()){
             if (sysUser.getUserParentId()!=null){
                 SysUserChannel parentUserChannel = sysUserChannelMapper.selectSysUserChannelById(sysUser.getUserParentId());
                 if (parentUserChannel==null){
                     throw new BusinessException("父级还未设置费率,请设置");
                 }
                 if (sysUserChannel.getUserChannelRate1().compareTo(parentUserChannel.getUserChannelRate1())<0){
                     throw new BusinessException("不能设置比父级还低的费率");
                 }
                 if (sysUserChannel.getUserChannelFee().compareTo(parentUserChannel.getUserChannelFee())<0){
                     throw new BusinessException("不能设置比父级还低的费率");
                 }
             }
        }
        return sysUserChannelMapper.updateSysUserChannel(sysUserChannel);
    }

    /**
     * 删除用户入金通道关系对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysUserChannelByIds(String ids)
    {
        return sysUserChannelMapper.deleteSysUserChannelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户入金通道关系信息
     *
     * @param userId 用户入金通道关系ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysUserChannelById(Long userId)
    {
        return sysUserChannelMapper.deleteSysUserChannelById(userId);
    }
}
